package com.zhaopin.weixinbook;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class TestReflect {

    @Test
    public void test1() throws ClassNotFoundException {

        Class<Bean> cls = Bean.class;
        Bean bean = new Bean();
        Class<? extends Bean> cls2 = bean.getClass();

        Class<?> cls3 = Class.forName("com.zhaopin.weixinbook.Bean");
        System.out.println(cls.toString());
        System.out.println(cls2.getName());

        System.out.println(cls3.getSimpleName());
        System.out.println(cls3.getCanonicalName());
        System.out.println(cls3.getPackage());

    }

    @Test
    public void test2() throws IllegalAccessException {
        List<String> obj = Arrays.asList(new String[]{"老马","说编程"});
        Class<? extends List> objClass = obj.getClass();
        for (Field declaredField : objClass.getDeclaredFields()) {
            declaredField.setAccessible(true);
            System.out.println(declaredField.getName() + "-" + declaredField.get(obj));

        }
    }

    @Test
    public void test3() throws IllegalAccessException, InstantiationException, NoSuchMethodException, ClassNotFoundException, InvocationTargetException {
        HashMap hashMap = HashMap.class.newInstance();
        hashMap.put("a","b");

        Class<?> aClass = Class.forName("com.zhaopin.weixinbook.Bean");
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class);
        Bean bean = (Bean)declaredConstructor.newInstance("aaaa");
        System.out.println(bean.getA());


        Constructor<StringBuilder> declaredConstructor1 = StringBuilder.class.getDeclaredConstructor(int.class);
        StringBuilder stringBuilder = declaredConstructor1.newInstance(300);
        stringBuilder.append("1111");
        System.out.println(stringBuilder.toString());

    }

    @Test
    public void test4() throws ClassNotFoundException {

        List list = new ArrayList();
        if(list instanceof ArrayList){
            System.out.println(true);
        }

        Class<?> aClass = Class.forName("java.io.Serializable");
        if (aClass.isInstance(list)) {
            Object cast = aClass.cast(list);
            System.out.println(cast.getClass());
        }


        System.out.println(aClass.isInterface());
        System.out.println(aClass.isArray());
        System.out.println(aClass.isAnnotation());
        System.out.println(aClass.isAnonymousClass());
        System.out.println(aClass.isEnum());
        System.out.println(aClass.isPrimitive());
        System.out.println(aClass.isMemberClass());
        System.out.println(aClass.isLocalClass());

        System.out.println(Object.class.isAssignableFrom(String.class));


    }

    @Test
    public void test5() throws ClassNotFoundException {

        String name = "[Ljava.lang.String;";
        Class<?> aClass = Class.forName(name);
        System.out.println(aClass == String[].class);

        String[] arr = new String[2];
        Class<?> componentType = arr.getClass().getComponentType();
        System.out.println(componentType);

    }
}

